package tianwang.utils.dict;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class RegularEntryProcesserImpl implements EntryProcesser {
	
	protected String splitString = "#";
	protected Map<String, Integer> entryTemplate = null;
	
	public RegularEntryProcesserImpl(){
		
	}
	public RegularEntryProcesserImpl(String splitString){
		this.splitString = ",";
	}
	
	public void setEntryTemplate(Map<String, Integer> entryTemplate) {
		this.entryTemplate = entryTemplate;
	}
	
	public Entry process(String entryInfo){
		if ( entryInfo == null ){
			//写入日志词条信息为空
			return null;
		}
		
		if( entryTemplate == null){
			//写入日志词条配置信息为空
			return null;
		}
		
		String[] info = entryInfo.split(this.splitString);
		if ( info.length != entryTemplate.size()){
			//写入日志词条信息与词条的配置信息不符
			return null;
		}
		
		Entry entry = new Entry();
		HashMap<String, String> items = new HashMap<String, String>();
		Set<String> keys = entryTemplate.keySet();
		for ( Iterator<String> itr=keys.iterator(); itr.hasNext();){
			String key = itr.next();
			int value = entryTemplate.get(key);
			if ( value >= 0 ) {
				items.put(key, info[value].trim());
			} else {
				//写入日志配置文件词条信息索引值有负值
				items.put(key, null);
			}			
		}
		entry.setItems(items);
		return entry;
	}
}
